package com.dididoctor.patient.Activity.Message.SystemMessage;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.MV.BaseFragment;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Ui.customlistview.OnRefreshListener;
import com.dididoctor.patient.Ui.customlistview.RefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： SystemMessageFragment
 * @包名称： com.dididoctor.patient.Activity.Message.SystemMessage
 * @创建人： zhanjunjie
 * @创建时间：2016/4/14 16:46
 * @修改人：
 * @修改时间：2016/4/14 16:46
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class SystemMessageFragment extends BaseFragment implements SystemMessageView {
    private Activity activity;
    private SystemMessagePresenter presenter;
    private RefreshListView listview;
    private SystemMessageAdapter adapter;
    private List<SystemMessage> messages = new ArrayList<>();

    private int pageIndex = 1;
    private String status;

    public SystemMessageFragment() {
    }

    @SuppressLint("ValidFragment")
    public SystemMessageFragment(String status) {
        this.status = status;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onAttach(Activity activity) {
        this.activity = activity;
        super.onAttach(activity);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_forum_module, null);
        this.initView(rootView);
        return rootView;

    }

    private void initView(View rootView) {
        listview = (RefreshListView) rootView.findViewById(R.id.lv_forum);
        adapter = new SystemMessageAdapter(getActivity(), messages);
        listview.setAdapter(adapter);
        listview.setHeadAndFoot(true, true);
        listview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                presenter.getmessageInfo(pageIndex);
            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
                presenter.getmessageInfo(pageIndex);
            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        presenter = new SystemMessagePresenter(getActivity(), this);
        presenter.getmessageInfo(pageIndex);
    }

    @Override
    public void getmessage(List<SystemMessage> messages) {
        if (messages != null && messages.size() > 0) {
            //没有评价内容
            if (pageIndex == 1) {
                this.messages.clear();
                adapter.setData(messages);
                listview.onRefreshFinish();
            } else {
                if (messages.size() < ConstantValue.LOAD_MESSAGE_COUNT) {
                    adapter.addData(messages);
                    listview.showNoMoreData();
                } else {
                    adapter.addData(messages);
                    listview.hideNoMoreData();
                }
            }
            adapter.notifyDataSetChanged();
        } else {
            if (pageIndex == 1) {
                listview.onRefreshFinish();
            } else {
                listview.showNoMoreData();
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void getPersonalmessage(List<PersonalBean> messages) {

    }

    @Override
    public void getmessagefail() {
        showToastMessage("网络连接失败，请检查您的网络设置");
        listview.onRefreshFinish();
    }

    @Override
    public void IntentInquiryActivity(String isCmp, String hosId, String doctorID, String addId) {

    }

    @Override
    public void IntentP2PActivity(String recId, String status, String hosId, String doctorID, String addId, String isSign,String names) {

    }

    @Override
    public void IntentP2PActivityGo() {

    }

    @Override
    public void IntentP2PActivityTwo() {

    }

    @Override
    public void getdoctorfail() {

    }
}
